/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : STM32F103 2.4-inch LCD (without touch) demo
  ******************************************************************************
  */
/* USER CODE END Header */
#include "main.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

#include "lcd_driver.h"
#include "lcd_gui.h"
#include <stdio.h>

void SystemClock_Config(void);
static void LCD_Graphics_Demo(void);

int main(void)
{
    HAL_Init();
    SystemClock_Config();

    MX_GPIO_Init();
    MX_SPI1_Init();
    MX_USART2_UART_Init();
    MX_TIM3_Init();

    printf("2.4-inch LCD STM32 demo (without touch)...\r\n");

    Lcd_Init();

    LCD_Clear(RED);
    HAL_Delay(600U);
    LCD_Clear(WHITE);
    HAL_Delay(600U);

    LCD_Graphics_Demo();

    while (1)
    {
        HAL_Delay(1000U);
    }
}

static void LCD_Graphics_Demo(void)
{
    Gui_draw_line(10, 1, 10, 239, RED, 2, SOLID);
    Gui_draw_line(15, 1, 15, 239, ORANGE, 2, SOLID);
    Gui_draw_line(20, 1, 20, 239, YELLOW, 2, SOLID);
    Gui_draw_line(25, 1, 25, 239, GREEN, 2, SOLID);
    Gui_draw_line(30, 1, 30, 239, GBLUE, 2, SOLID);
    Gui_draw_line(35, 1, 35, 239, BLUE, 2, SOLID);
    Gui_draw_line(40, 1, 40, 239, PURPLE, 2, SOLID);

    Gui_draw_line(70, 10, 319, 10, BLACK, 1, SOLID);
    Gui_draw_line(70, 20, 319, 20, RED, 2, DOTTED);
    Gui_draw_line(70, 30, 319, 30, YELLOW, 3, SOLID);
    Gui_draw_line(70, 40, 319, 40, MAGENTA, 4, DOTTED);

    Gui_draw_rectangle(70, 55, 100, 85, RED, 1, EMPTY);
    Gui_draw_rectangle(110, 55, 140, 85, GREEN, 1, EMPTY);
    Gui_draw_rectangle(150, 55, 180, 85, BLUE, 1, EMPTY);

    Gui_fill_color(200, 55, 230, 85, RED);
    Gui_fill_color(240, 55, 270, 85, GREEN);
    Gui_fill_color(280, 55, 310, 85, BLUE);

    Gui_draw_str(70, 100, "0123456789", &Font24, BLACK, WHITE);
    Gui_draw_str(70, 140, "SEENGREAT!", &Font24, RED, WHITE);
    Gui_draw_str(70, 180, "2.4inch LCD Display!", &Font24, BLUE, WHITE);
}

void SystemClock_Config(void)
{
    RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

    RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
    RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
    if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    {
        Error_Handler();
    }

    RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
                                  RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

    if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
    {
        Error_Handler();
    }
}

void Error_Handler(void)
{
    __disable_irq();
    while (1)
    {
    }
}

#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{
    (void)file;
    (void)line;
}
#endif
